package com.yang.my.sync.three;

/**
 * 对象锁的同步和异步问题
 *
 * @Author: Mu_YI
 * @Date: 2019/3/17 20:53
 * @Version 1.0
 */
public class ObjectThread {

    private synchronized void methodOne(){
        try {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void methodTwo(){
        System.out.println(Thread.currentThread().getName());
    }

    /**
     *  t1线程先持有object对象的Lock锁，t2线程可以以异步的方式调用对象中的非synchronized修饰的方法
     *  t1线程先持有object对象的Lock锁，t2线程如果在这个时候调用对象中的同步（synchronized）方法则需等待，也就是同步
     * @param args
     */
    public static void main(String[] args) {
        ObjectThread objectThread = new ObjectThread();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                objectThread.methodOne();
            }
        },"t1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                objectThread.methodTwo();
            }
        },"t2");

        t1.start();
        t2.start();
    }

}
